package com.z.admin.controller;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import com.z.base.vo.PageVo;
import com.z.base.vo.Result;
import com.z.base.web.BaseController;
import com.z.blog.entity.Blog;
import com.z.blog.entity.Tag;
import com.z.blog.service.IBlogService;
import com.z.blog.service.ITagService;
import com.z.blog.service.ITypeService;
import com.z.utils.ImageBase64Converter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

@RestController
@RequestMapping("/admin/blog")
public class AdminBlogController extends BaseController {

    private String prefix = "admin/blog";

    @Autowired
    private IBlogService blogService;
    @Autowired
    private ITypeService typeService;
    @Autowired
    private ITagService tagService;

    @GetMapping("/main")
     public ModelAndView main(Model model) {
        model.addAttribute("types", typeService.list());
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询blog列表
     */
    @ResponseBody
    @GetMapping("/data")
    public Result list(@ModelAttribute Blog blog, PageVo pageVo, String s) {
        PageInfo<Blog> pageInfo = null;
        if(StringUtils.isEmpty(s)){
            pageInfo = blogService.selectBlogPage(blog, pageVo);
        }else {
            blog.setTitle(s);
            blog.setDescription(s);
            blog.setContent(s);
            pageInfo = blogService.selectBlogPage(blog, pageVo);
        }
        return Result.table(pageInfo);
    }

    /**
     * 新增blog
     */
    @GetMapping("/add")
    public ModelAndView add(Model model) {
        model.addAttribute("types", typeService.list());
        model.addAttribute("tags", tagService.list());
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增blog
     */
    @ResponseBody
    @PostMapping("/save")
    public Result save(@RequestBody Blog blog) {
        blog.setCreateTime(new Date());
        blog.setViews(0L);
        return Result.decide(blogService.save(blog));
    }

    /**
     * 修改blog
     */
    @GetMapping("/edit")
    public ModelAndView edit(Long id, ModelMap map) {
        Blog blog =blogService.getById(id);
        blog.setUrl(ImageBase64Converter.convertFileToBase64(blog.getFirstPicture()));
        map.put("blog", blog);
        map.put("types", typeService.list());

        List<Integer> tagIds = tagService.getIdsByBlogId(id);

        List<Tag> tags = tagService.list();
        tags.forEach(o ->  {
            if(tagIds.contains(o.getId()))
                o.setSelected(true);
        });

        map.put("tags", tags);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改blog
     */
    @ResponseBody
    @PutMapping("/update")
    public Result update(@RequestBody Blog blog) {
        blog.setUpdateTime(new Date());
        return Result.decide(blogService.updateById(blog));
    }

    /**
     * 删除blog
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    public Result batchRemove(String ids) {
        return Result.decide(blogService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除blog
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable("id") int id) {
        return Result.decide(blogService.deleteById(id));
    }
}